package com.gxa.insurance.service.impl;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.mapper.DriverMapper;
import com.gxa.insurance.pojo.Driver;
import com.gxa.insurance.service.DriverService;
import com.gxa.insurance.util.StringUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Min
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class DriverServiceImpl implements DriverService {
    @Autowired
    private DriverMapper mapper;
    @Override
    public List<Driver> selectAllEmail() {
        return mapper.selectAllEmail();
    }

    @Override
    public Integer add(Driver driver) throws Exception {
        Integer rows = mapper.insert(driver);
        if(rows==0){
            throw new BusinessException(ExceptionCode.INSERT_FILED);
        }
        return rows;
    }

    @Override
    public Map<String, Object> getDriverlist(Integer page, Integer size) {
        Map<String, Object> map = new HashMap<>();
        List<Driver> drivers = mapper.selectByPage((page - 1) * size, size);
        Integer count = mapper.selectCount();

        map.put("drivers",drivers);
        map.put("count",count);

        return map;
    }

    @Override
    public List<Driver> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public Map<String, Object> getLikeList(Map map) {

        List<Driver> drivers = null;
        Map<String,Object> listMap = new HashMap();
        Object page = map.get("page");
        Object size = map.get("size");
        int intPage = Integer.parseInt(String.valueOf(page));
        int intPageSize = Integer.parseInt(String.valueOf(size));
        intPage=(intPage-1)*intPageSize;
        map.put("page",intPage);
        map.put("size",intPageSize);
        if(StringUtil.isEmpty((String)map.get("name"))){
            drivers = mapper.selectByPage(intPage, intPageSize);
        }
        drivers = mapper.selectlikeName(map);

        String name = (String)map.get("name");
        Driver driver = new Driver();
        driver.setName(name);
        driver.setPhone(name);
        Integer count = mapper.selectLikeCount(driver);

        listMap.put("drivers",drivers);
        listMap.put("count",count);
        return listMap;
    }

    @Override
    public Integer update(Integer id,Integer status) throws Exception {
        Integer update = mapper.update(id, status);
        if(update==0){
            throw  new BusinessException(ExceptionCode.UPDATE_FILED);
        }
        return update;
    }

    @Override
    public Map<String,Object> login(String phone, String pwd) throws Exception {
        Map<String,Object> map = new HashMap();
        if(StringUtil.isEmpty(phone)||StringUtil.isEmpty(pwd)){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        Driver driver = mapper.selectByPhoneAndPwd(phone, pwd);
        if(driver==null){
            throw new BusinessException(ExceptionCode.USER_NOT_FONUD);
        }
        map.put("driverScore","4.5");
        map.put("orderNum","968");
        map.put("goodComment","90.0%");
        map.put("driver",driver);
        return map;
    }

    @Override
    public Boolean selectByPhoneAndPwd(String phone, String pwd) throws Exception {
        Driver driver = mapper.selectByPhoneAndPwd(phone, pwd);
        if(driver==null){
            throw new BusinessException(ExceptionCode.USER_NOT_FONUD);
        }

        return true;
    }

    @Override
    public Integer updatePwd(String phone, String pwd) throws BusinessException {

        Integer integer = mapper.updatePwd(phone, pwd);
        if(integer==0){
            throw new BusinessException(ExceptionCode.UPDATE_FILED);
        }
        return integer;
    }

    @Override
    public Integer updatePhone(String beforePhone, String nowPhone) throws BusinessException {
        Integer integer = mapper.updatePhone(beforePhone, nowPhone);
        if(integer==0){
            throw new BusinessException(ExceptionCode.UPDATE_FILED);
        }
        return integer;
    }
}
